import { AttributeType } from './AttributeType';
import { AttributeModifier } from './AttributeModifier';
import { ModifierType } from './ModifierType';
import { ModifierSource } from './ModifierSource';
import { EquipmentConfig, EquipmentConfigTable } from './EquipmentConfig';

export class Equipment {
    private config: EquipmentConfig;
    private modifiers: Map<AttributeType, AttributeModifier[]>;

    constructor(configId: string) {
        const configTable = EquipmentConfigTable.getInstance();
        const config = configTable.getConfig(configId);
        if (!config) {
            throw new Error(`Equipment config not found: ${configId}`);
        }
        this.config = config;
        this.modifiers = this.createModifiersFromConfig();
    }

    public getConfig(): EquipmentConfig {
        return this.config;
    }

    public getModifiers(): Map<AttributeType, AttributeModifier[]> {
        return this.modifiers;
    }

    private createModifiersFromConfig(): Map<AttributeType, AttributeModifier[]> {
        const modifiers = new Map<AttributeType, AttributeModifier[]>();
        
        for (const [type, effects] of this.config.effects) {
            const typeModifiers = effects.map(effect => new AttributeModifier(
                effect.type,
                effect.value,
                effect.source,
                -1, // permanent
                effect.priority,
                effect.basedOnAttribute
            ));
            modifiers.set(type, typeModifiers);
        }
        
        return modifiers;
    }

    // 创建一个基于血量百分比的装备
    public static createHpBasedAttackEquipment(name: string, hpPercentageToAttack: number): Equipment {
        const config: EquipmentConfig = {
            id: `hp_based_attack_${Date.now()}`,
            name,
            description: `攻击力增加当前生命值的${hpPercentageToAttack * 100}%`,
            effects: new Map([
                [AttributeType.Attack, [{
                    type: ModifierType.PERCENTAGE,
                    value: hpPercentageToAttack,
                    source: ModifierSource.EQUIPMENT
                }]]
            ])
        };

        const configTable = EquipmentConfigTable.getInstance();
        configTable.addConfig(config);
        return new Equipment(config.id);
    }
} 